
function getRadioValue(radioList){

	for(var i=0;i<radioList.length;i++){
		if(radioList[i].checked == true)
			return radioList[i].value;
	}
	return null;
};

function init(){
	dojo.connect(dojo.byId("insertRow"),"onclick", function(){
		dojo.byId("submitbutton").value = "insert";
	});
	dojo.connect(dojo.byId("updateRow"),"onclick", function(){
		dojo.byId("submitbutton").value = "update";
		var radioList = dojo.query("#resulttb .modify");
		var cols = dojo.query("#pkcols .col");
		if (cols.length == 0) {
			alert("There are no primary keys in this table!");
		}
		else
			if (radioList.length == 0 || getRadioValue(radioList) == null) {
				alert("Please select one row!");
			}
			else {
				var sql = "";
				for(var i=0; i<cols.length; i++) {
					sql += cols[i].value+"='"+dojo.query("."+cols[i].value)[getRadioValue(radioList)].value+"' and ";
				}
				sql = sql.substring(0,sql.length-5);
				var url = "ModifyRow?uid="+document.getElementsByName("uid")[0].value+"&uAccess="+
								document.getElementsByName("uAccess")[0].value+"&submitbutton=update"
								+"&tName="+dojo.byId("tName").value+"&sql="+encodeURIComponent(sql);
				window.location.href = url;
				/*
	           	var x = dojo.xhrPost({
				      url: url,
				      error: function(response, ioArgs) {
				        alert("ERROR:"+ response);
				      },
					  sync: true
				   });
				   */
			}
	});
	dojo.connect(dojo.byId("deleteRow"),"onclick", function(){
		dojo.byId("submitbutton").value = "delete";
		var radioList = dojo.query("#resulttb .modify");
		var cols = dojo.query("#pkcols .col");
		if(cols.length == 0) {
			alert("There are no primary keys in this table!");
		}
		else if (radioList.length == 0 || getRadioValue(radioList)==null) {
			alert("Please select one row!");
		}
		else {
			if(confirm("Are You Sure to Delete This Row?")==true){

				var sql = "delete from "+dojo.byId("tName").value+" where ";
				for(var i=0; i<cols.length; i++) {
					sql += cols[i].value+"='"+dojo.query("."+cols[i].value)[getRadioValue(radioList)].value+"' and ";
				}
				sql = sql.substring(0,sql.length-5);
				var url = "ModifyRow?confirm=Y&uid="+document.getElementsByName("uid")[0].value+"&uAccess="+
								document.getElementsByName("uAccess")[0].value+"&submitbutton=delete"
								+"&sql="+encodeURIComponent(sql);

	           	var x = dojo.xhrGet({
				      url: url,
				      sync: true,
				      load: function(response, ioArgs) {
				         window.location.reload();
				      },
				      error: function(response, ioArgs) {
				        alert("ERROR:"+ response);
				      },
					  sync: true
				   });
			}

		}
	});


};

dojo.addOnLoad(init);